<!DOCTYPE html>
<html class="x-admin-sm" lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>任务统一调度平台</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>

    <link rel="icon" th:href="@{../static/layui/images/logo.png}">
    <link rel="stylesheet" th:href="@{../static/layui/css/font.css}">
    <link rel="stylesheet" th:href="@{../static/layui/css/xadmin.css}">
    <script th:src="@{../static/layui/js/jquery.min.js}"></script>
    <script th:src="@{../static/js/CommonUtil.js}"></script>
    <script th:src="@{../static/js/kconfig.js}"></script>
    <script th:src="@{../static/layui/lib/layui/layui.js}" charset="utf-8"></script>
    <script th:src="@{../static/layui/js/xadmin.js}" charset="utf-8"></script>
    <script th:src="@{../static/layui/js/md5.js}" charset="utf-8"></script>


    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form action="" method="post" class="layui-form layui-form-pane">
            <input type="hidden" id="id" name="id"/>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">
                    用户权限
                </label>
                <table class="layui-table layui-input-block">
                    <tbody>
                    <tr>
                        <td>
                            <input type="checkbox" name="menus" class="" lay-skin="primary"
                                   value="userManger"
                                   lay-filter="father"
                                   title="用户管理">
                        </td>
                        <td>
                            <div class="layui-input-block">
                                <input name="functions" lay-skin="primary" type="checkbox" value="userAdd"
                                       title="添加用户">
                                <input name="functions" lay-skin="primary" type="checkbox" value="userUpdate"
                                       title="修改用户">
                                <input name="functions" lay-skin="primary" type="checkbox" value="userUpdatePwd"
                                       title="修改密码">
                                <input name="functions" lay-skin="primary" type="checkbox" value="userUpdatePower"
                                       title="修改权限">
                                <input name="functions" lay-skin="primary" type="checkbox" value="userDelete"
                                       title="删除用户">
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <input name="menus" lay-skin="primary" type="checkbox" value="taskManger" title="任务管理">
                        </td>
                        <td>
                            <div class="layui-input-block">
                                <input name="functions" lay-skin="primary" type="checkbox" value="taskAdd"
                                       title="添加任务">
                                <input name="functions" lay-skin="primary" type="checkbox" value="taskUpdate"
                                       title="修改任务">
                                <input name="functions" lay-skin="primary" type="checkbox" value="taskChangeStatus"
                                       title="启动|停用任务">
                                <input name="functions" lay-skin="primary" type="checkbox" value="taskDelete"
                                       title="删除任务">
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <input name="menus" lay-skin="primary" type="checkbox" value="taskLogManger" title="日志管理">
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>
            <div class="layui-form-item">
                <button class="layui-btn" lay-submit="" lay-filter="add">保存</button>
            </div>
        </form>
    </div>
</div>
<script>
    layui.use(['form', 'layer'], function () {
        const $ = layui.jquery;
        const form = layui.form;
        const layer = layui.layer;
        const reqParams = RequestParameter();
        if (null == reqParams) {
            history.back();
        }
        const userId = reqParams['id'];
        if (null == userId) {
            history.back();
        }

        AjaxRequest.aPost(api.user.detail, JSON.stringify({id: userId}), function (r) {
                if (r.code === 0) {
                    const data = r.data;
                    $("#id").val(data.id);
                    const menuKeys = data.menus;
                    const functionKeys = data.functions;
                    if (null !== menuKeys && '' !== menuKeys) {
                        const menuKeysArray = menuKeys.split(",");
                        for (let j = 0; j < menuKeysArray.length; j++) {
                            const menusCheckbox = $("input[name='menus']");
                            for (let i = 0; i < menusCheckbox.length; i++) {
                                if (menusCheckbox[i].value === menuKeysArray[j]) {
                                    menusCheckbox[i].checked = true;
                                }
                            }
                        }
                    }
                    if (null !== functionKeys && '' !== functionKeys) {
                        const functionKeysArray = functionKeys.split(",");
                        for (let j = 0; j < functionKeysArray.length; j++) {
                            const functionsCheckbox = $("input[name='functions']");
                            for (let i = 0; i < functionsCheckbox.length; i++) {
                                if (functionsCheckbox[i].value === functionKeysArray[j]) {
                                    functionsCheckbox[i].checked = true;
                                }
                            }
                        }
                    }
                    form.render();
                } else {
                    if (r.msg !== null && r.msg !== '') {
                        ResponseMsg.errorMsg('查看详权限详情失败，' + r.msg);
                    } else {
                        ResponseMsg.errorMsg('查看详权限详情失败，请重试。');
                    }
                }
            }
        );

        //监听提交
        form.on('submit(add)', function (data) {
            const menus = [];
            $('input[name="menus"]:checked').each(function (index, domEle) {
                menus.push($(this).val());
            });

            const functions = [];
            $('input[name="functions"]:checked').each(function (index, domEle) {
                functions.push($(this).val());
            });

            const jsonParam = JSON.stringify({
                id: data.field.id,
                menus: menus,
                functions: functions
            });
            AjaxRequest.aPost(api.user.update_power, jsonParam, function (r) {
                if (r.code === 0) {
                    // 登入成功的提示与跳转
                    layer.msg('修改成功', {
                        offset: '15px', icon: 1, time: 500
                    }, function () {
                        // 获得frame索引
                        const index = parent.layer.getFrameIndex(window.name);
                        //关闭当前frame
                        parent.layer.close(index);
                        //刷新父级页面的表格
                        window.parent.location.reload();
                    });
                } else {
                    if (r.msg !== null && r.msg !== '') {
                        ResponseMsg.errorMsg('修改失败，' + r.msg);
                    } else {
                        ResponseMsg.errorMsg('添加失败，请重试。');
                    }
                }
            });
            return false;
        });

        form.on('checkbox(father)', function (data) {
            if (data.elem.checked) {
                $(data.elem).parent().siblings('td').find('input').prop("checked", true);
                form.render();
            } else {
                $(data.elem).parent().siblings('td').find('input').prop("checked", false);
                form.render();
            }
        });
    });
</script>
</body>
</html>